package com.cunyme;

import java.io.IOException;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class Blackboard_instructor extends Activity
{
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

		SQLiteDatabase cuny_meDB = openOrCreateDatabase("cuny_me.db", Context.MODE_PRIVATE, null);
        helper_functions helpers = new helper_functions();
		Map <String, String> blackboard_cookies = helpers.getcookies("blackboard", cuny_meDB);
		cuny_meDB.close();
		
		// This creates the main layout...
    	ScrollView instructor_layout = new ScrollView(this);
    	instructor_layout.setBackgroundColor(0xffffffff);
    	LinearLayout child_layout = new LinearLayout(this); 

    	child_layout.setOrientation(LinearLayout.VERTICAL);
    	child_layout.setBackgroundColor(0xffffffff);
    	LinearLayout.LayoutParams bboard_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
    			LinearLayout.LayoutParams.WRAP_CONTENT); 
		instructor_layout.addView(child_layout);
		
		try
		{
			Connection instructor_conn = Jsoup.connect("https://bbhosted.cuny.edu/bin/common/staff.pl").data("action","LIST", "content_id", getIntent().getExtras().getString("content_id")).cookie("JSESSIONID", blackboard_cookies.get("JSESSIONID")).cookie("session_id", blackboard_cookies.get("session_id")).cookie("ObSSOCookie", blackboard_cookies.get("ObSSOCookie")).cookie("NSC_100211.qspe2_wjq2:443", blackboard_cookies.get("NSC_100211.qspe2_wjq2:443")).cookie("BIGipServerbbhosted_ssl_pl", blackboard_cookies.get("BIGipServerbbhosted_ssl_pl"));
			org.jsoup.nodes.Document instructor_doc = instructor_conn.post();
			String instructor_html = instructor_doc.html();
			
			
			String[] ins_str_manip = instructor_html.split("personalinfo_on.gif\" /> Staff Information");
			ins_str_manip[1] = ins_str_manip[1].replaceAll("</a></font>", "</a></font><br />");
			ins_str_manip[1] = ins_str_manip[1].replaceAll("<b", "beebeesnurzxy");
			ins_str_manip[1] = ins_str_manip[1].replaceAll("</b", "kookoosnurzxy");
			ins_str_manip[1] = ins_str_manip[1].replaceAll("<a", "gobblegobblegobblezzz");
			ins_str_manip[1] = ins_str_manip[1].replaceAll("</a", "commacommacommazzz");
			ins_str_manip[1] = ins_str_manip[1].replaceAll("<br />", "kookookoozzz");
			ins_str_manip[1] = ins_str_manip[1].replaceAll("<.*?>", "");
			ins_str_manip[1] = ins_str_manip[1].replaceAll("kookookoozzz", "<br />");
			ins_str_manip[1] = ins_str_manip[1].replaceAll("gobblegobblegobblezzz", "<a");
			ins_str_manip[1] = ins_str_manip[1].replaceAll("commacommacommazzz", "</a");
			ins_str_manip[1] = ins_str_manip[1].replaceAll("beebeesnurzxy", "<b");
			ins_str_manip[1] = ins_str_manip[1].replaceAll("kookoosnurzxy", "</b");
			
			TextView ins_content = new TextView(this);
			ins_content.setText(Html.fromHtml(ins_str_manip[1]));
			ins_content.setMovementMethod(LinkMovementMethod.getInstance());
			ins_content.setId(6);
			ins_content.setLayoutParams(bboard_params);
			ins_content.setTextColor(0xff000000);
			child_layout.addView(ins_content);

			setContentView(instructor_layout);
		} 
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(getApplicationContext(), "Unable to Establish Connection.  Please try again.", Toast.LENGTH_SHORT).show();	
			finish();
		}
    }
}
